﻿using System.Diagnostics;
using System.IO;
using System.ComponentModel;

namespace Toybox.SCM
{
	using Models.Accounts;
	using Models.Configs;

	/// <summary>
	/// Data
	/// </summary>
	public static class StaticData
	{

		#region [NotifyPropertyChanged]

		/// <summary>
		/// Property Changed Event
		/// </summary>
		public static event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		/// Fire Property Changed Event
		/// </summary>
		/// <param name="propertyName"></param>
		private static void OnPropertyChanged(string propertyName = null)
		{
			if (PropertyChanged != null)
				PropertyChanged(typeof(StaticData), new PropertyChangedEventArgs(propertyName));
		}

		#endregion [NotifyPropertyChanged]


		#region [Const]

		private const string DIRECTORY_PATH_CONFIG = @"config";

		private const string FILENAME_UI = @"uiconfig.xml";
		private const string FILENAME_EXTRA = @"extraconfig.xml";

		private const string FILENAME_ACCOUNT = @"accounts.csv";
		private const string FILENAME_GROUP = @"groups.csv";

		#endregion [Const]


		#region Public Members

		/// <summary>
		/// Exe Directory Path
		/// </summary>
		public static string DirectoryPath
		{
			get
			{
				return _directoryPath
					?? (_directoryPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));
			}
		}
		private static string _directoryPath;

		/// <summary>
		/// Accounts
		/// </summary>
		public static AccountCollection Accounts
		{
			get { return _accounts; }
			set
			{
				if (_accounts == value) return;
				_accounts = value;
				OnPropertyChanged("Accounts");
			}
		}
		private static AccountCollection _accounts;

		/// <summary>
		/// Groups
		/// </summary>
		public static AccountGroupCollection Groups
		{
			get { return _groups; }
			set
			{
				if (_groups == value) return;
				_groups = value;
				OnPropertyChanged("Groups");
			}
		}
		private static AccountGroupCollection _groups;

		/// <summary>
		/// UIConfig
		/// </summary>
		public static UIConfig UIConfig
		{
			get { return _uiConfig; }
			set
			{
				if (_uiConfig == value) return;
				_uiConfig = value;
				OnPropertyChanged("UIConfig");
			}
		}
		private static UIConfig _uiConfig;

		/// <summary>
		/// ExtraConfig
		/// </summary>
		public static ExtraConfig ExtraConfig
		{
			get { return _extraConfig; }
			set
			{
				if (_extraConfig == value) return;
				_extraConfig = value;
				OnPropertyChanged("ExtraConfig");
			}
		}
		private static ExtraConfig _extraConfig;

		#endregion Public Members


		#region Public Methods

		/// <summary>
		/// Load StaticData
		/// </summary>
		public static void Load()
		{

			string fileUI = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_UI);
			string fileExtra = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_EXTRA);

			string fileAccount = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_ACCOUNT);
			string fileGroup = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_GROUP);

			StaticData.UIConfig = UIConfig.Load(fileUI);
			StaticData.ExtraConfig = ExtraConfig.Load(fileExtra);

			StaticData.Accounts = AccountCollection.Load(fileAccount);
			StaticData.Groups = AccountGroupCollection.Load(fileGroup, StaticData.Accounts);
		}

		/// <summary>
		/// Save StaticData
		/// </summary>
		public static void Save()
		{
			string fileUI = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_UI);
			string fileExtra = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_EXTRA);

			string fileAccount = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_ACCOUNT);
			string fileGroup = Path.Combine(DirectoryPath, DIRECTORY_PATH_CONFIG, FILENAME_GROUP);

			UIConfig.Save(fileUI, StaticData.UIConfig);
			ExtraConfig.Save(fileExtra, StaticData.ExtraConfig);

			AccountCollection.Save(fileAccount, StaticData.Accounts);
			AccountGroupCollection.Save(fileGroup, StaticData.Groups);
		}

		#endregion Public Methods

	}
}
